#!/bin/bash
# 一键执行完整分析流程

set -e  # 遇到错误立即退出

echo "🚀 启动基金/股票分析流程"
echo "========================="

# 检查参数
if [ $# -ne 3 ]; then
    echo "用法: $0 <symbol> <type> <start_date>"
    echo "示例: $0 016837 fund 2023-01-01"
    echo "类型: stock/fund/etf"
    exit 1
fi

SYMBOL=$1
TYPE=$2
START_DATE=$3

echo "📊 分析参数:"
echo "  代码: $SYMBOL"
echo "  类型: $TYPE"
echo "  开始日期: $START_DATE"
echo ""

# 创建输出目录
mkdir -p ../csv_output

# 步骤1: 数据提取
echo "🔍 步骤1: 数据提取..."
python3 1_extract_data.py --symbol "$SYMBOL" --type "$TYPE" --start "$START_DATE"
if [ $? -ne 0 ]; then
    echo "❌ 数据提取失败"
    exit 1
fi

# 步骤2: 技术指标计算
echo "📈 步骤2: 技术指标计算..."
python3 2_calculate_technical.py
if [ $? -ne 0 ]; then
    echo "❌ 技术指标计算失败"
    exit 1
fi

# 步骤3: 风险收益分析
echo "⚖️  步骤3: 风险收益分析..."
python3 3_calculate_risk.py
if [ $? -ne 0 ]; then
    echo "❌ 风险收益分析失败"
    exit 1
fi

# 步骤4: 估值分析
echo "💰 步骤4: 估值分析..."
python3 4_calculate_valuation.py
if [ $? -ne 0 ]; then
    echo "❌ 估值分析失败"
    exit 1
fi

# 步骤5: 综合评分
echo "🎯 步骤5: 综合评分..."
python3 5_generate_score.py
if [ $? -ne 0 ]; then
    echo "❌ 综合评分失败"
    exit 1
fi

echo ""
echo "✅ 分析完成！"
echo "📋 查看结果:"
echo "  cat ../csv_output/level5_final_score.csv"

# 可选：清理临时文件
# rm -f ../csv_output/temp_*.csv

echo ""
echo "🎉 流程执行成功！"